package com.viphuli.app.tool.adapter;

import java.util.List;

import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.fragment.CircleMemberFragment;
import com.viphuli.app.tool.utils.ImageUtils;

public class HomeMemberAdapter extends BaseAdapter {

	private Fragment frag;
	private int totalCount;

	//界面列表  
	private List<AccountUser> userList;

	public HomeMemberAdapter(Fragment frag, List<AccountUser> userList, int totalCount) {
		this.userList = userList;
		this.frag = frag;
		this.totalCount = totalCount;
	}

	@Override
	public int getCount() {
		return userList.size();
	}

	@Override
	public Object getItem(int index) {
		return userList.get(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null || convertView.getTag() == null) {
			convertView = ViewUtils.inflateView(MyApp.getInstance(), R.layout.frag_member_girdview_item);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		AccountUser user = userList.get(position);

		if (!user.getUid().equals("-1")) {

			if (frag instanceof CircleMemberFragment) {

				if (((CircleMemberFragment) frag).isEdit()) {

					holder.del.setVisibility(View.VISIBLE);

				} else {

					holder.del.setVisibility(View.GONE);
				}

			}

		}

		if (user.getUid().equals("-1")) {

			holder.nameText.setText("添加成员");
			holder.portraitImage.setImageResource(R.drawable.sl_member_add);

		} else if (user.getUid().equals("-2")) {

			holder.nameText.setText("全部成员");
			holder.portraitImage.setImageResource(R.drawable.ic_member_more);
			holder.portraitText.setVisibility(View.VISIBLE);
			holder.portraitText.setText(totalCount + "人");
			int size = (int) frag.getResources().getDimension(R.dimen.sp_common_body_text_14_size);
			holder.nameText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);

		} else {

			holder.nameText.setText(user.getUserName());
			ImageUtils.load(holder.portraitImage, user.getPortrait());

		}

		return convertView;
	}

	protected class ViewHolder {

		@Bind(R.id.id_circle_member_girdview_item_portrait)
		ImageView portraitImage;

		@Bind(R.id.id_circle_member_girdview_item_portrait_text)
		TextView portraitText;

		@Bind(R.id.id_circle_member_girdview_item_name)
		TextView nameText;

		@Bind(R.id.id_circle_member_girdview_item_del)
		ImageView del;

		public ViewHolder(View view) {
			ButterKnife.bind(this, view);
		}
	}

}
